package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.ExperimentDetail;
import com.siwei.mes.mapper.ExperimentDetailMapper;
import com.siwei.mes.service.ExperimentDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账json详情(ExperimentDetail)表服务实现类
 *
 * @author linzi
 * @since 2023-12-27 23:30:45
 */
@Service
public class ExperimentDetailServiceImpl implements ExperimentDetailService {
    @Resource
    private ExperimentDetailMapper experimentDetailMapper;

    @Override
    public Long create(ExperimentDetail entity) {
        entity.init(true);
        experimentDetailMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentDetail entity) {
        ExperimentDetail experimentDetail = experimentDetailMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentDetail == null, "试验台账json详情信息不存在");
        experimentDetail.init(false);
        experimentDetailMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentDetail experimentDetail = experimentDetailMapper.selectByPrimaryKey(id);
        assertTrue(experimentDetail == null, "试验台账json详情信息不存在");
        experimentDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentDetail> getPage(PageQuery<ExperimentDetail, ExperimentDetail> query) {
        ExperimentDetail searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentDetailMapper.getList(searchParam));
    }

    @Override
    public ExperimentDetail loadById(Long id) {
        return experimentDetailMapper.loadById(id);
    }

    @Override
    public List<ExperimentDetail> getAll(ExperimentDetail entity) {
        return experimentDetailMapper.getList(entity);
    }
}
